package com.xyh.system.controller;

import com.xyh.common.annotation.RefreshFilterChain;
import com.xyh.common.util.ResultCode;
import com.xyh.common.util.ResultVO;
import com.xyh.system.model.Menu;
import com.xyh.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyle
 * @since 2021-01-05
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    /**
     * 跳转至menu-list界面
     *
     * @return
     */
    @GetMapping("/index")
    public String toMenuIndex() {
        return "menu/menu-list";
    }

    /**
     * 获取菜单树
     *
     * @return
     */
    @GetMapping("/tree")
    @ResponseBody
    public ResultVO getMenuTree() {

        List<Menu> menus = menuService.selectAllTree();
        return ResultVO.success(menus);
    }

    /**
     * 根据父级id获取该菜单下的所有子集菜单
     *
     * @param parentId
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultVO getChildMenuByParentId(@RequestParam(required = false) Integer parentId) {
        List<Menu> childMenus = menuService.selectChildMenuByParentId(parentId);

        return ResultVO.success(childMenus);
    }

    /**
     * 获取菜单树+自定义的根节点->导航目录
     *
     * @return
     */
    @GetMapping("/tree/root")
    @ResponseBody
    public ResultVO getTreeAndRoot() {
        return ResultVO.success(menuService.getAllMenuTreeAndRoot());
    }

    /**
     * 查询带功能权限的菜单树,并组装根节点
     *
     * @return Menu菜单树集合
     */
    @GetMapping("/tree/root/operator")
    @ResponseBody
    public ResultVO getAllMenuAndCountOperatorTreeAndRoot() {
        return ResultVO.success(menuService.getAllMenuAndCountOperatorTreeAndRoot());
    }

    /**
     * 调整菜单排序
     *
     * @param currentId 需要调整的菜单id
     * @param swapId    与之交换的菜单id
     */
    @PostMapping("/swap")
    @ResponseBody
    public ResultVO swapSort(@RequestParam("currentId") Integer currentId, @RequestParam("swapId") Integer swapId) {
        menuService.swapSort(currentId, swapId);
        return ResultVO.success();
    }

    /**
     * 跳转至menu-add新增界面
     *
     * @return
     */
    @GetMapping
    public String toAdd() {
        return "menu/menu-add";
    }

    /**
     * 新增菜单节点，并且刷新过滤器链
     *
     * @param menu
     * @return
     */
    @RefreshFilterChain
    @PostMapping
    @ResponseBody
    public ResultVO add(Menu menu) {
        boolean b = menuService.insert(menu);
        if (b) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 跳转到菜单修改页面(和新增页面一致)
     *
     * @param menuId
     * @param model
     * @return
     */
    @GetMapping("/{menuId}")
    public String toUpdate(@PathVariable("menuId") Integer menuId, Model model) {
        Menu menu = menuService.getById(menuId);//mybatis-plus自带方法
        model.addAttribute("menu", menu);
        return "menu/menu-add";
    }

    /**
     * 更新菜单节点,并且刷新过滤器链
     *
     * @param menu
     * @return
     */
    @RefreshFilterChain
    @PutMapping
    @ResponseBody
    public ResultVO update(Menu menu) {
        boolean b = menuService.updateById(menu);//mybatis-plus自带方法
        if (b) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }

    /**
     * 删除菜单节点，如果存在下级节点则一并删除
     *
     * @param menuId
     * @return
     */
    @DeleteMapping("/{menuId}")
    @ResponseBody
    public ResultVO delete(@PathVariable("menuId") Integer menuId) {
        boolean b = menuService.removeByParentIdEqMenuId(menuId);
        if (b) {
            return ResultVO.success();
        } else {
            return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
        }
    }
}
